package com.fuzongle.tenant.utils;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther: fuzongle
 * @date: 2021-03-30
 * @description: String字符串 处理工具类
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 将驼峰命名的类名转换为下划线开头的字符串
     *
     * @param string 待转换的字符串
     * @return 转换后的字符串
     */
    public static String underscoreName(String string) {
        StringBuilder sBuilder = new StringBuilder();
        Optional<String> strOptional = Optional.ofNullable(string);
        if (strOptional.isPresent()) {
            for (char c : string.toCharArray()) {
                if (Character.isUpperCase(c)) {
                    sBuilder.append("_");
                    c = Character.toLowerCase(c);
                }
                sBuilder.append(c);
            }
        }
        return sBuilder.substring(1);
    }
    /**
     * 去掉指定字符串的开头的指定字符
     * @param stream 原始字符串
     * @param trim 要删除的字符串
     * @return
     */
    public static String StringStartTrim(String stream, String trim) {
        // null或者空字符串的时候不处理
        if (stream == null || stream.length() == 0 || trim == null || trim.length() == 0) {
            return stream;
        }
        // 要删除的字符串结束位置
        int end;
        // 正规表达式
        String regPattern = "[" + trim + "]*+";
        Pattern pattern = Pattern.compile(regPattern, Pattern.CASE_INSENSITIVE);
        // 去掉原始字符串开头位置的指定字符
        Matcher matcher = pattern.matcher(stream);
        if (matcher.lookingAt()) {
            end = matcher.end();
            stream = stream.substring(end);
        }
        // 返回处理后的字符串
        return stream;
    }


}
